package com.tbit.main.controller;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.SysConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserLastPos;
import com.tbit.main.pojo.TerPosition;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 运维轨迹
 *
 * @author LMY
 * @create 2021-03-22 17:04
 */
@RestController
@RequestMapping("/accountUserIdController")
public class AccountUserHistoryController {

    @Autowired
    private HistoryService historyService;
    @Autowired
    private AccountUserLastPosService accountUserLastPosService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private GeoreGionService georeGionService;
    @Autowired
    private AccountUserHistoryService accountUserHistoryService;

    /**
     * 插入运维人员轨迹
     */
    @RequestMapping("/insert")
    public JsonResult insert(String token, Integer accountId, Double lon, Double lat, Integer mapType) {
        Assert.notNull(lon, "Map.lon.nutNull");
        Assert.notNull(lat, "Map.lat.nutNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        accountUserHistoryService.insert(token, accountId, lon, lat, mapType);
        return JsonResult.succ();
    }

    /**
     * 根据时间查询单个运维轨迹
     */
    @RequestMapping("/getByTime")
    public JsonResult getByTime(String token, Integer accountUserId, String startTime, String endTime, Integer mapType) {
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountUserId, "AccountUser.id.notNull");

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        AccountUser accountUser = accountUserService.getById(accountUserId);

        if (accountUser == null) {
            throw new BaseException("AccountUser.notExist");
        }

        List<TerPosition> terPositions = historyService.getByAccountUserPhoneAndTime(mapType, accountUser.getPhone(), startTime, endTime);

        return JsonResult.succ(terPositions);
    }

    /**
     * 查询单个运维最后位置
     *
     * @param token
     * @param accountUserId
     * @param mapType
     * @return
     */
    @RequestMapping("/lastPos")
    public JsonResult lastPos(String token, Integer accountUserId, Integer mapType) {
        Assert.notNull(accountUserId, "AccountUser.notExist");

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        AccountUserLastPos accountUserLastPos = accountUserLastPosService.getByAccountUserId(accountUserId);

        if (accountUserLastPos != null) {
            calibrateService.calibrateLastPos(accountUserLastPos, mapType);
        }

        return JsonResult.succ(accountUserLastPos);
    }

    /**
     * 查询所有运维最后位置
     *
     * @param token
     * @param accountId
     * @param mapType
     * @return
     */
    @RequestMapping("/allLastPos")
    public JsonResult allLastPos(String token, Integer accountId, Integer mapType,
                                 @RequestParam(value = "regionId", required = false) List<Integer> regionId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        if (CollUtil.isEmpty(regionId)) {
            regionId = georeGionService.getByAccountUserId(accountUser.getAccountUserId());
        }

        List<AccountUserLastPos> accountUserLastPos = accountUserLastPosService.getByAccountId(accountId, accountUser.getAccountUserType(), accountUser.getAccountUserId(), regionId);

        if (accountUserLastPos != null) {
            calibrateService.calibrateLastPosS(accountUserLastPos, mapType);
        }

        return JsonResult.succ(accountUserLastPos);
    }

}
